<?php
/**
 * Created by PhpStorm.
 * User: Sergey
 * Date: 22.02.14
 * Time: 16:40
 */

include plugin_dir_path( __FILE__ ) . "Config.php";
include plugin_dir_path( __FILE__ ) . "ConnectionFactory.php";

class Installer extends ConnectionFactory
{
    public function __construct()
    {
    }

    public function removeTable($tableName)
    {
        $query = "DROP TABLE IF EXISTS $tableName;";
        $this->query($query);
    }

    public function createGroupsTable()
    {
        $table_name = Config::get()->getGroupsTableName();
        $query = "
        CREATE TABLE IF NOT EXISTS $table_name (
            id INT(9) NOT NULL AUTO_INCREMENT,
            name tinytext NOT NULL,
            UNIQUE KEY id (id)
        );";
        global $wpdb;
        $wpdb->query($query);
    }

    public function removeGroupsTable()
    {
        $this->removeTable(Config::get()->getGroupsTableName());
    }

    public function createQuizzesTable()
    {
        $table_name = Config::get()->getQuizzesTableName();
        $query = "
        CREATE TABLE IF NOT EXISTS $table_name (
            id INT(9) NOT NULL AUTO_INCREMENT,
            name TINYTEXT NOT NULL,
            description TEXT,
            start_date DATE NOT NULL,
            end_date DATE NOT NULL,
            tutor_id INT(9) NOT NULL,
            UNIQUE KEY id (id)
        );";
        $this->query($query);
    }

    public function removeQuizzesTable()
    {
        $this->removeTable(Config::get()->getQuizzesTableName());
    }

    public function createQuestionsTable()
    {
        $tableName = Config::get()->getQuestionsTableName();
        $query = "
        CREATE TABLE IF NOT EXISTS $tableName (
            id INT(9) NOT NULL AUTO_INCREMENT,
            content TEXT NOT NULL,
            quiz_id INT(9) NOT NULL,
            UNIQUE KEY id (id)
        )";
        global $wpdb;
        $wpdb->query($query);
    }

    public function removeQuestionsTable()
    {
        $this->removeTable(Config::get()->getQuestionsTableName());
    }

    public function createAnswersTable() {
        $tableName = Config::get()->getAnswersTableName();
        $query = "
        CREATE TABLE IF NOT EXISTS $tableName (
            id INT(9) NOT NULL AUTO_INCREMENT,
            content TEXT NOT NULL,
            is_correct BIT NOT NULL,
            question_id INT(9) NOT NULL,
            UNIQUE KEY id (id)
        )";
        global $wpdb;
        $wpdb->query($query);
    }

    public function removeAnswersTable()
    {
        $this->removeTable(Config::get()->getAnswersTableName());
    }

    public function createRelQuizToGroupsTable()
    {
        $tableName = Config::get()->getRelQuizToGroupsTableName();
        $query = "
        CREATE TABLE IF NOT EXISTS $tableName (
            quiz_id INT(9) NOT NULL,
            group_id INT(9) NOT NULL,
            UNIQUE KEY id (quiz_id, group_id)
        )";
        $this->query($query);
    }

    public function removeRelQuizToGroupsTable()
    {
        $this->removeTable(Config::get()->getRelQuizToGroupsTableName());
    }

    public function createRelTutorToGroupsTable()
    {
        $tableName = Config::get()->getRelTutorToGroupsTableName();
        $query = "
        CREATE TABLE IF NOT EXISTS $tableName (
            user_id INT(9) NOT NULL,
            group_id INT(9) NOT NULL,
            UNIQUE KEY id (user_id, group_id)
        )";
        $this->query($query);
    }

    public function removeRelTutorToGroupsTable()
    {
        $this->removeTable(Config::get()->getRelTutorToGroupsTableName());
    }

    public function createRelStudentToGroupTable()
    {
        $tableName = Config::get()->getRelStudentToGroupTableName();
        $query = "
        CREATE TABLE IF NOT EXISTS $tableName (
            user_id INT(9) NOT NULL,
            group_id INT(9) NOT NULL,
            UNIQUE KEY id (user_id)
        )";
        $this->query($query);
    }

    public function removeRelStudentToGroupTable()
    {
        $this->removeTable(Config::get()->getRelStudentToGroupTableName());
    }

    public function createResultsTable()
    {
        $tableName = Config::get()->getResultsTableName();
        $query = "
        CREATE TABLE IF NOT EXISTS $tableName (
            quiz_id INT(9) NOT NULL,
            question_id INT(9) NOT NULL,
            answers VARCHAR(256) NOT NULL,
            user_id INT(9) NOT NULL,
            UNIQUE KEY id (quiz_id,question_id,user_id)
        )";
        $this->query($query);
    }

    public function removeResultsTable()
    {
        $this->removeTable(Config::get()->getResultsTableName());
    }
} 